import 'package:dio/dio.dart';

class OrderAPI {
  final Dio _dio;

  OrderAPI(this._dio);

  // 订单确认
  // isNew = 0, 来在购物车， isNew = 1, 来自立即购买
  Future<dynamic> confirm(String cartId, [int isNew = 0]) async {
    FormData formData = FormData.fromMap({
      "cartId": cartId,
      "new": isNew
    });
    Response res = await _dio.post('/order/confirm', data: formData);

    return res.data;
  }

  /// 获取订单价格（订单价格会根据各种因素计算所得）
  /// 
  /// String key         订单信息唯一标识
  /// int    addressId   收获地址
  /// String payType     支付方式
  /// int    couponId    优惠券 Id
  /// int    useIntegral 是否使用积分抵扣
  Future<dynamic> price({ 
    required String key,
    required int addressId,
    required String payType,
    int couponId = 0,
    int useIntegral = 0
  }) async {
    FormData formData = FormData.fromMap({
      "addressId": addressId,
      "payType": payType,
      "couponId": couponId,
      "useIntegral": useIntegral,
    });
    Response res = await _dio.post('/order/computed/'+key.toString(), data: formData);

    return res.data;
  }

  /// 创建订单
  /// 
  /// String key         订单信息唯一标识
  /// int    addressId   收货地址 Id
  /// String payType     支付方式
  /// int    couponId    优惠券 Id
  /// int    useIntegral 是否使用积分抵扣
  /// String remark      订单备注
  Future<dynamic> create({ 
    required String key,
    required int addressId,
    required String payType,
    int couponId = 0,
    int useIntegral = 0,
    String remark = ""
  }) async {
    FormData formData = FormData.fromMap({
      "addressId": addressId,
      "payType": payType,
      "couponId": couponId,
      "useIntegral": useIntegral,
      "mark": remark
    });
    Response res = await _dio.post('/order/create/'+key.toString(), data: formData);

    return res.data;
  }

  /// 返回订单详情
  Future<dynamic> detail(String uni) async {
    Response res = await _dio.get('/order/detail/'+uni);

    return res.data;
  }

  /// 返回订单列表
  Future<dynamic> list({int type = 1, int page = 1, int limit = 8}) async {
    Response res = await _dio.get('/order/list',
      queryParameters: {
        "type": type,
        "page": page,
        "limit": limit
      }
    );

    return res.data;
  }

  /// 添加评论 - 对订单中的单个商品进行评论
  Future<dynamic> comment({ 
    required String unique,
    required String comment,
    double product_score = 0,
    double service_score = 0,
  }) async {
    FormData formData = FormData.fromMap({
      "unique": unique,
      "comment": comment,
      "product_score": product_score,
      "service_score": service_score,
    });
    Response res = await _dio.post('/order/comment', data: formData);

    return res.data;
  }
}